package com.aismy.admin.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author Aismy
 * @date 2025/03/15
 **/
@Component
@RequiredArgsConstructor
public class Json {

    private static ObjectMapper mapper;

    private final ObjectMapper injectedObjectMapper;

    @PostConstruct
    public void init() {
        mapper = injectedObjectMapper;
    }

    public static <T> T parse(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
